/**
 * 
 */
package org.youisoft.summer.evaluator.function.core;

import org.youisoft.summer.evaluator.InvokeException;
import org.youisoft.summer.evaluator.UnitEnum;
import org.youisoft.summer.evaluator.context.CalContext;
import org.youisoft.summer.evaluator.function.AbstractFunction;
import org.youisoft.summer.evaluator.operator.Operator;
import org.youisoft.summer.evaluator.operator.base.ClosureOperator;
import org.youisoft.summer.evaluator.operator.base.ListOperator;
import org.youisoft.summer.evaluator.unit.Parenthesis;

/**
 * @author zijiang.jl
 *
 */
public class ParenthesisFunction extends AbstractFunction{

	@Override
	public String getName() {
		return Parenthesis.class.getName();
	}
	
	
	
	@Override
	protected boolean checkParenthesisOperator(int index, Operator<?> operator) {
		return true;
	}



	@Override
	public UnitEnum getType() {
		return UnitEnum.parenthesis;
	}



	@Override
	public <C, R, P> Operator<?> doParenthesisInvoke(CalContext<C, R, P> context,
			Operator<?>... operator) throws InvokeException {
		if(operator.length==0) return Operator.nullInstance;
		if(operator.length==1) {
			if(operator[0] instanceof ClosureOperator){
				ClosureOperator co=ClosureOperator.class.cast(operator[0]);
				co.execute(context);
			}else{
				return operator[0];
			}
		
		}
		ListOperator<Operator<?>> lo=new ListOperator<Operator<?>>();
		for(Operator<?> op:operator){
			if(op instanceof ClosureOperator){
				ClosureOperator co=ClosureOperator.class.cast(op);
				co.execute(context);
			}else{
				lo.addOperator(op);
			}
			
		}
		return lo;
	}
}
